package cn.py.boot_lua.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;

import java.lang.reflect.Method;

/**
 * 异步线程报错处理
 */
public class AsyncError implements AsyncUncaughtExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(AsyncError.class);

    @Override
    public void handleUncaughtException(Throwable ex, Method method, Object... params) {
        Thread currentThread = Thread.currentThread();
        String className = method.getDeclaringClass().getSimpleName();
        String methodName = method.getName();

        // 构建详细的日志信息
        log.error("=== 异步任务未捕获异常 ===", ex);
        log.error("线程名称: {}", currentThread.getName());
        log.error("异常类: {}", ex.getClass().getSimpleName());
        log.error("异常信息: {}", ex.getMessage());
        log.error("异常位置: {}.{}()", className, methodName);

        // 打印参数
        if (params.length == 0) {
            log.error("参数列表: 无");
        } else {
            for (int i = 0; i < params.length; i++) {
                Object param = params[i];
                log.error("参数[{}]: {}", i, param != null ? param.toString() : "null");
            }
        }
        log.error("=== 异常结束 ===");
    }
}
